<?php
/**
 */
class Yz_Pagination_Default
    extends Yz_Object
    implements Yz_Pagination_Interface
{

    const PAGE_ITEMS = 10;

    protected $pageItems;
    protected $pageNumber;
    protected $pageCount;
    protected $itemCount;

    /*********************************
     ***      Public methods      ****
     *********************************/

    /** constructor accepts the page number and optional number of items per page
     *
     *
     */
    public function __construct($pageNumber, $pageItems=null)
    {
        $this->setPageNumber($pageNumber);
        if (null === $pageItems) {
            $this->pageItems = static::PAGE_ITEMS;
        } else {
            $this->setPageItems($pageItems);
        }
    }

    /**
     *  get the current page number
     *  @return int
     */
    public function getPageNumber()
    {
        return $this->pageNumber;
    }

    /**
     *  set the current page number
     *  @param int $pageNumber > 0
     *  @return chainable
     */
    public function setPageNumber($pageNumber)
    {
        $this->pageNumber = Yz_Int::testPositive($pageNumber);
        return $this;
    }

    /** Get the total number of pages in the list (table)
     *  @return int
     */
    public function getPageCount()
    {
        debug("Items: $this->itemCount");
        return ceil($this->itemCount/$this->pageItems);
    }

    /** Get the total number of items in the list (table)
     *  @return int
     */
    public function getItemCount()
    {
        return $this->itemCount;
    }

    /** set the total number of items in the list (table)
     *  @param int $itemCount >= 0
     *  @return chainable
     */
    public function setItemCount($itemCount)
    {
        $this->itemCount = Yz_Int::testNotNegative($itemCount);
        return $this;
    }

    /** Get the number of items per page
     *  @return int
     */
    public function getPageItems()
    {
        return $this->pageItems;
    }
    /** set the number of items per page
     *  @param int $pageItems > 0 |probably also 0, which means 'all items'
     *  @return chainable
     */
    public function setPageItems($pageItems)
    {
        $this->pageItems = Yz_Int::testPositive($pageItems);
        return $this;
    }

    /** Get the number of the first item on current page (suitable for SQL OFFSET clause)
     *  @return int
     */
    public function getPageOffset()
    {
        if (null === $this->pageNumber) {
            return null;
        }
        return $this->pageItems * ($this->pageNumber-1);
    }
}
